package com.jingdianjichi.project.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.project.application.convert.SubjectAnswerDTOConverter;
import com.jingdianjichi.project.application.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.project.application.dto.SubjectInfoDTO;
import com.jingdianjichi.project.common.entity.PageResult;
import com.jingdianjichi.project.common.entity.Result;
import com.jingdianjichi.project.domain.bo.SubjectAnswerBo;
import com.jingdianjichi.project.domain.bo.SubjectInfoBo;
import com.jingdianjichi.project.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.project.domain.service.SubjectLikedDomainService;
import com.jingdianjichi.project.infra.basic.entity.SubjectInfo;
import com.jingdianjichi.project.infra.basic.entity.SubjectInfoEs;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:SubjectController
 * Package:com.jingdianjichi.subject.application.controller
 * Description:
 *
 * @Author fst
 * @Create 2024/9/28 19:10
 * @Version 1.0
 */


@RestController
@RequestMapping("/subject/")
@Slf4j
public class SubjectController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;


    @Resource
    private RocketMQTemplate rocketMQTemplate;




    @PostMapping("add")
    public Result add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            //断言检查
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(!(CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds())), "分类id不能为空");
            Preconditions.checkNotNull(!(CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds())), "标签id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDTOConverter.INSTANCE.convertDTOToBOsubject(subjectInfoDTO);

            //对类型进行转化
            List<SubjectAnswerBo> subjectAnswerBoList =
                    SubjectAnswerDTOConverter.INSTANCE.
                            convertDTOTOBOAnswerList
                                    (subjectInfoDTO.getOptionList());

            subjectInfoBo.setOptionList(subjectAnswerBoList);

            subjectInfoDomainService.add(subjectInfoBo);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLabelController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            //断言检查
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "id不能为空");


            SubjectInfoBo subjectInfoBo = SubjectInfoDTOConverter.INSTANCE.convertDTOToBOsubject(subjectInfoDTO);
            SubjectInfoBo subjectInfobo= subjectInfoDomainService.querySubjectInfo(subjectInfoBo);

            SubjectInfoDTO DTOResult = SubjectInfoDTOConverter.INSTANCE.convertBOTODTOSubject(subjectInfobo);


            log.info(" SubjectInfoDTO:{}",DTOResult);
            return Result.ok(DTOResult);

        } catch (Exception e) {
            log.error("SubjectLabelController.querySubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            //断言检查
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");


            SubjectInfoBo subjectInfoBo = SubjectInfoDTOConverter.INSTANCE.convertDTOToBOsubject(subjectInfoDTO);
            PageResult<SubjectInfoBo> boPageResult =subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            return Result.ok(boPageResult);

        } catch (Exception e) {
            log.error("SubjectLabelController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            //断言检查
            Preconditions.checkArgument(subjectInfoDTO.getKeyWord()!=null&&!subjectInfoDTO.getKeyWord().equals(""),
                    "搜索关键字不能为空");


            SubjectInfoBo subjectInfoBo = SubjectInfoDTOConverter.INSTANCE.convertDTOToBOsubject(subjectInfoDTO);

            subjectInfoBo.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBo.setPageSize(subjectInfoDTO.getPageSize());

            PageResult<SubjectInfoEs> boPageResult =subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBo);
            return Result.ok(boPageResult);


        } catch (Exception e) {
            log.error("SubjectLabelController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList(@RequestBody SubjectInfoDTO subjectInfoDTO)
    {
        try {
            List<SubjectInfoBo> subjectInfoBOList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> subjectInfoDTOList = SubjectInfoDTOConverter.INSTANCE.convertBOTODTOSubjectList(subjectInfoBOList);
            return Result.ok(subjectInfoDTOList);
        }catch (Exception e)
        {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }

    }

    //测试mq发送

    @PostMapping("/pushMessage")
    public Result<Boolean> pushMessage(@Param("id") int id)
    {
        rocketMQTemplate.convertAndSend("first-topic","鸡翅早上好"+id);
        return Result.ok();
    }
}
